package com.guigu.tms.base.service.impl;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guigu.tms.base.mapper.GoodsTypeMapper;
import com.guigu.tms.base.mapper.TruckTypeGoodsTypeMapper;
import com.guigu.tms.base.mapper.TruckTypeMapper;
import com.guigu.tms.base.service.IGoodsTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guigu.tms.commons.base.entity.GoodsType;
import com.guigu.tms.commons.base.entity.TruckType;
import com.guigu.tms.commons.base.entity.TruckTypeGoodsType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 * 货物类型表 服务实现类
 * </p>
 *
 * @author zhh
 * @since 2022-09-23
 */
@Service
public class GoodsTypeServiceImpl extends ServiceImpl<GoodsTypeMapper, GoodsType> implements IGoodsTypeService {
@Autowired
GoodsTypeMapper goodsTypeMapper;
@Autowired
TruckTypeMapper truckTypeMapper;
@Autowired
TruckTypeGoodsTypeMapper truckTypeGoodsTypeMapper;


    @Override
    public PageInfo<GoodsType> GoodsTypeQuery(String name, String TruckTypeId,Integer num, Integer size) {
        PageHelper.startPage(num==null?1:num,size==null?5:size);
        List<GoodsType> goodsTypes=null;
        if (StringUtils.isEmpty(TruckTypeId)) {
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.like("name", name);
            goodsTypes=goodsTypeMapper.selectList(queryWrapper);
        }else {
            goodsTypes = goodsTypeMapper.GoodsTypeQuery(name, TruckTypeId);
        }
            for (int i = 0; i < goodsTypes.size(); i++) {
                GoodsType goodsType = goodsTypes.get(i);
                System.out.println(goodsType.getId());
                List<TruckType> truckTypes = truckTypeMapper.queryTruckName(String.valueOf(goodsType.getId()));
                goodsTypes.get(i).setTruckName(truckTypes);
            }
            System.out.print(goodsTypes);
            return new PageInfo<GoodsType>(goodsTypes);
        }


    @Override
    //事务注解使代码出错回滚
    @Transactional
    public int deleteGoodsType(int id) {
        //货品类型表删除
        int res=goodsTypeMapper.deleteById(id);
        //货品类型与车辆管理表删除
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("goods_type_id",id);
        truckTypeGoodsTypeMapper.delete(queryWrapper);
        return res;
    }

    @Override
    @Transactional
    public boolean updateById(GoodsType goodsType) {
        //修改前先把车辆类型全部删除
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("goods_type_id",goodsType.getId());
        truckTypeGoodsTypeMapper.delete(queryWrapper);

        goodsTypeMapper.updateById(goodsType);

        //重新取值添加
        List<Integer> truckTypeGoodsTypeId = goodsType.getTruckTypeGoodsTypeId();
        if (truckTypeGoodsTypeId!=null) {
            for (Integer id : truckTypeGoodsTypeId)  {
                TruckTypeGoodsType truckTypeGoodsType = new TruckTypeGoodsType();
                truckTypeGoodsType.setGoodsTypeId(String.valueOf(goodsType.getId()));
                truckTypeGoodsType.setTruckTypeId(Integer.toString(id));
                truckTypeGoodsTypeMapper.insert(truckTypeGoodsType);
            }
        }
        return true;
    }

    @Override
    @Transactional
    public boolean save(GoodsType entity) {
    //id自动生成
        String id1 = IdUtil.getSnowflake().nextIdStr();
        entity.setGoodsTypeId(id1);
        goodsTypeMapper.insert(entity);


        List<Integer> truckTypeGoodsTypeId = entity.getTruckTypeGoodsTypeId();
        for (Integer id : truckTypeGoodsTypeId) {
            TruckTypeGoodsType truckTypeGoodsType=new TruckTypeGoodsType();
            truckTypeGoodsType.setGoodsTypeId(String.valueOf(entity.getId()));
            truckTypeGoodsType.setTruckTypeId(Integer.toString(id));
            truckTypeGoodsTypeMapper.insert(truckTypeGoodsType);
        }
        return true;
    }
}
